package com.controller;

import com.api.CommonResult;
import com.entity.Develop;
import com.entity.table.DevelopTableDef;
import com.entity.table.NarrativeTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Narrative;
import com.service.NarrativeService;

import java.io.Serializable;
import java.util.List;

/**
 * 文学记述 控制层。
 *
 * @author admin
 * @since 2024-06-25
 */
@Controller
@RequestMapping("/admin/narrative")
public class NarrativeController {

    @Autowired
    private NarrativeService narrativeService;

    @GetMapping(value = "/narrative")
    public String attName(ModelMap model, @RequestParam(value = "narrativeName", defaultValue = "") String narrativeName,
                          @RequestParam(value = "p",defaultValue = "1") int p,
                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        //按照用户名模糊查找
        try {
            QueryWrapper queryWrapper = QueryWrapper.create().where(NarrativeTableDef.NARRATIVE.NARRATIVENAME.like(narrativeName));
            Page<Narrative> page = new Page<>();
            page.setPageSize(pageSize); //设置每页显示记录数
            page.setPageNumber(p);  //设置页码
            Page<Narrative> pageInfo = narrativeService.getMapper().paginateWithRelations(page, queryWrapper);
            model.addAttribute("page", pageInfo);
            model.addAttribute("total",pageInfo.getTotalRow());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "back/Narrative-list";
    }

    @GetMapping(value = "/addView")
    public String addView() {return "back/Narrative-add";}

    /**
     * 添加起源。
     *
     * @param narrative 起源
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ResponseBody
    public CommonResult save(Narrative narrative) {
        try {
            boolean r = narrativeService.save(narrative);
            return  CommonResult.success(r);
        }catch (Exception e) {
            e.printStackTrace();
            String msg = narrative.getNarrativename()+"已存在";
            return CommonResult.failed(msg);
        }
    }

    //添加修改
    @GetMapping(value = "updateView")
    public String updateView(int id, Model model){
        Narrative narrative = narrativeService.getById(id);
        model.addAttribute("narrative", narrative);
        return "back/Narrative-edit";
    }

    /**
     * 根据主键更新起源。
     *
     * @param narrative 起源
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(Narrative narrative){
        try {
            boolean r = narrativeService.updateById(narrative);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            String msg = narrative.getNarrativename()+"已存在";
            return CommonResult.failed(msg);
        }
    }

    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id){
        try {
            boolean r = narrativeService.removeById(id);
            return CommonResult.success(r);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }

}
